var mysql = require('mysql');
let connection = null;
export function createMysqlConnection (configObj, databaseName) {
    connection = mysql.createConnection({
        host: "localhost",
        port: "3306",
        user: "root",
        password: "123456",
        database: databaseName
    });
    return connection;
}
export function runMysqlSql (sqlStr) {
    let tableObj = {
        tableColumns: [],
        tableData: []
    };
    connection.connect();
    connection.query(sqlStr, function (err, result) {
        if (err) {
            console.log(err);
            return;
        }
        if(result.length > 0) {
            tableObj.tableColumns = [];
            let j = {};
            j.type = 'selection';
            j.width = 50;
            j.align = 'center';
            tableObj.tableColumns.push(j);
            for (var i = 0; i < 1; i++) {
                let keys = Object.keys(result[i]);
                for (let key of keys) {
                    let j = {};
                    j.title = key;
                    j.key = key;
                    tableObj.tableColumns.push(j);
                }
            }
            tableObj.tableData = [];
            for (let obj of result) {
                tableObj.tableData.push(obj);
            }
            console.log("tableObj 1" + tableObj.tableColumns)
        }
    });
    console.log("tableObj 2" + tableObj.tableColumns)
    connection.end();
    return tableObj;
}